package com.jia.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jia.dto.admin.ActivityDto;
import com.jia.entity.Activity;
import com.jia.entity.Coupons;
import com.jia.service.ActivityService;
import com.jia.mapper.ActivityMapper;
import lombok.experimental.Accessors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
* @author 贾天旺
* @description 针对表【t_activity】的数据库操作Service实现
* @createDate 2024-04-25 20:51:56
*/
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>
    implements ActivityService{


    @Autowired
    private ActivityMapper activityMapper;
    @Override
    public Page<Activity> findAll(Integer page, Integer limit, ActivityDto activityDto) {
        if(ObjectUtil.isNotEmpty(activityDto.getTitle())){
            return activityMapper.selectPage(new Page<Activity>(page,limit),new QueryWrapper<Activity>().like("title",activityDto.getTitle()).eq("state",activityDto.getState()));
        }else if(ObjectUtil.isNotEmpty(activityDto.getCreateTimeBegin()) && ObjectUtil.isNotEmpty(activityDto.getCreateTimeEnd())){
            return activityMapper.selectPage(new Page<Activity>(page,limit),new QueryWrapper<Activity>().between("createtime",activityDto.getCreateTimeBegin(),activityDto.getCreateTimeEnd()).eq("state",activityDto.getState()));
        }else if(ObjectUtil.isNotEmpty(activityDto.getTitle()) && ObjectUtil.isNotEmpty(activityDto.getCreateTimeBegin()) && ObjectUtil.isNotEmpty(activityDto.getCreateTimeEnd())){
            return activityMapper.selectPage(new Page<Activity>(page,limit),new QueryWrapper<Activity>().like("title",activityDto.getTitle()).between("createtime",activityDto.getCreateTimeBegin(),activityDto.getCreateTimeEnd()).eq("state",activityDto.getState()));
        }else{
            return activityMapper.selectPage(new Page<Activity>(page,limit),new QueryWrapper<Activity>().eq("state",activityDto.getState()));
        }
    }

    @Override
    public void updateState(int id) {
        Activity activity = this.getById(id);
        if (ObjectUtil.isNotEmpty(activity)) {
            activity.setState(activity.getState() == 1 ? 0 : 1);
            this.updateById(activity);
        }
    }
}




